package com.sm.healthy.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.sm.healthy.utils.CacheUtil
import com.sm.healthy.R
import com.sm.healthy.adapter.UserListAdapter
import com.sm.healthy.base.BaseFragment
import com.sm.healthy.databinding.FragmentUserCenterListBinding
import com.sm.healthy.ext.clearData
import com.sm.healthy.model.UserCenterViewModel
import io.freespiritsocial.user.ext.init
import kotlinx.android.synthetic.main.fragment_user_center_list.*
import kotlinx.android.synthetic.main.title_layout.*
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

// TODO: Rename parameter arguments, choose names that match
/**
 * A simple [Fragment] subclass.
 * Use the [UserCenterListFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class UserCenterListFragment : BaseFragment<UserCenterViewModel, FragmentUserCenterListBinding>() {

    private val adapter by lazy(LazyThreadSafetyMode.NONE) {
        UserListAdapter().apply {
            addChildClickViewIds(R.id.tv_del)
            setOnItemChildClickListener { adapter, view, position ->
                mDatabind.llBtn.visibility = View.VISIBLE
            }
            setOnItemClickListener { adapter, view, position ->
                appViewModel.userinfo.value = data[position]
            }
            setOnItemLongClickListener { adapter, view, position ->
                false
            }
        }
    }

    override fun layoutId(): Int {
        return R.layout.fragment_user_center_list
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.rvPerson.init(adapter)
    }

    override fun lazyLoadData() {
        mViewModel.getCustomers().observe(this) {
            adapter.clearData()
            adapter.addData(it)
            adapter.curSelectPosition = adapter.data.indexOfFirst {
                it.id == appViewModel.userinfo.value?.id
            }
        }
        appViewModel.userinfo.observe(this) { userEntity ->
            CacheUtil.setCurUser(userEntity)
            adapter.curSelectPosition = adapter.data.indexOfFirst {
                it.id == appViewModel.userinfo.value?.id
            }
        }
//        mViewModel.getUserList(true) {
//            adapter.addData(it)
//        }
        mDatabind.click = Click()
    }

    inner class Click {
        fun backUp() {
            nav().navigateUp()
        }

        fun addUser() {
            nav().navigateAction(R.id.action_userCenterListFragment_to_userCenterFragment)
        }

        fun cancel() {
            adapter.selectPosition = -1
            mDatabind.llBtn.visibility = View.INVISIBLE
        }

        fun deleteItem() {
            val userEntity = adapter.data[adapter.selectPosition]
            mViewModel.deleteUser(userEntity)
            adapter.remove(userEntity)
            if (adapter.selectPosition == adapter.curSelectPosition) {
                adapter.curSelectPosition = -1
                appViewModel.userinfo.value = null
            }
            adapter.selectPosition = -1
            mDatabind.llBtn.visibility = View.INVISIBLE
        }
    }

}